package com.alfayun.fiscalriskanalysis.indicatorspec;

import java.util.Collection;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.alfayun.fiscalriskanalysis.util.TagsConverter;

@Entity
@Table(name = "indicator_spec")
public final class IndicatorSpec {

	@Column(name = "indicator_spec_id", updatable = false)
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Id
	private Integer id;
	public String name;
	public String description;
	@Convert(converter = TagsConverter.class)
	public Set<String> tags;
	
	@OneToMany(mappedBy = "indicatorSpecId", targetEntity = SelectorSpec.class)
	private Set<SelectorSpec> selectors;
	@OneToMany(mappedBy = "indicatorSpecId", targetEntity = MeasureSpec.class)
	private Set<MeasureSpec> measures;
	@OneToMany(mappedBy = "indicatorSpecId", targetEntity = AlertSpec.class)
	private Set<AlertSpec> alerts;
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	public String getTags() {
		return TagsConverter.joinSet(tags);
	}

	public void setTags(String tags) {
		this.tags = TagsConverter.splitString(tags);
	}

	public Collection<SelectorSpec> getAllSelectors() {
		return selectors;
	}

	public Set<MeasureSpec> getAllMeasures() {
		return measures;
	}

	public Set<AlertSpec> getAllAlerts() {
		return alerts;
	}
	
	public static IndicatorSpec newIndicatorSpec(IndicatorSpecRepository repository, IndicatorSpec indicatorSpec) {
		indicatorSpec.id = 0;

		return repository.save(indicatorSpec);
	}
}
